/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.network.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import com.hyts.network.base.ServerFactoryBean;
import com.hyts.network.config.DefaultNetwork;

/** 
 * @author LiBo/Alex
 * @see com.hyts.network.base.ServerFactoryBean<ServerSocketChannel>
 * @since V1.0
 * @version V1.0 
 */
public abstract class NewServerFactoryBean<T> extends ServerFactoryBean<ServerSocketChannel> {

    /**  
     * <p>是否属于阻塞模式：默认为false</p> 
     * @fieldName isBlock
     * @fieldType boolean
     */ 
    private boolean isBlock;
    
    /**  
     * <p>最大连接数</p> 
     * @fieldName backlog
     * @fieldType int
     */ 
    private int backlog;


    private T threadPool;

    /**  
     * @name backlog's getter method
     * @return backlog
     */
    public int getBacklog() {
        return backlog;
    }

    /**  
     * @name backlog's setter method
     * @param backlog
     * @return void
     */
    public void setBacklog(int backlog) {
        this.backlog = backlog;
    }

    /**  
     * @name isBlock's getter method
     * @return isBlock
     */
    public boolean isBlock() {
        return isBlock;
    }

    /**  
     * @name isBlock's setter method
     * @param isBlock
     * @return void
     */
    public void setBlock(boolean isBlock) {
        this.isBlock = isBlock;
    }

    /**********************************com.hyts.ext.network.base.ServerFactoryBean<ServerSocketChannel>**new creator**************************************************/
    
    /**  
     * @constructor：NewServerFactoryBean  
     * @param port
     * @param server  
     */ 
    public NewServerFactoryBean(int port, ServerSocketChannel server) {
        super(port, server);
        this.isBlock = DefaultNetwork.DEFAULT_SOCKET_IS_BLOCK;
    }

    /**  
     * @constructor：NewServerFactoryBean  
     * @param server
     * @param singleCpuThreadNum
     * @param tryReconnectedTimes  
     */ 
    public NewServerFactoryBean(ServerSocketChannel server, int singleCpuThreadNum, int tryReconnectedTimes) {
        super(server, singleCpuThreadNum, tryReconnectedTimes);
        this.setPort(DefaultNetwork.DEFAULT_NETWORK_LOCAL_PORT);
        this.isBlock = DefaultNetwork.DEFAULT_SOCKET_IS_BLOCK;
    }

    /**  
     * @constructor：NewServerFactoryBean  
     * @param server
     * @param singleCpuThreadNum  
     */ 
    public NewServerFactoryBean(ServerSocketChannel server, int singleCpuThreadNum) {
        super(server, singleCpuThreadNum);
        this.setPort(DefaultNetwork.DEFAULT_NETWORK_LOCAL_PORT);
        this.isBlock = DefaultNetwork.DEFAULT_SOCKET_IS_BLOCK;
    }

    /**  
     * @constructor：NewServerFactoryBean  
     * @param server  
     */ 
    public NewServerFactoryBean(ServerSocketChannel server) {
        super(server);
        this.setPort(DefaultNetwork.DEFAULT_NETWORK_LOCAL_PORT);
        this.isBlock = DefaultNetwork.DEFAULT_SOCKET_IS_BLOCK;
    }
    /**********************************com.hyts.ext.network.server.NewServerFactoryBean**new creator**************************************************/
    
    /**  
     * @throws IOException 
     * @constructor：NewServerFactoryBean    
     */ 
    public NewServerFactoryBean() throws IOException {
        //默认为false 非阻塞型
        super((ServerSocketChannel) ServerSocketChannel.open().bind(new InetSocketAddress
                (DefaultNetwork.DEFAULT_NETWORK_LOCAL_PORT)).configureBlocking(DefaultNetwork.DEFAULT_SOCKET_IS_BLOCK));
        this.isBlock = DefaultNetwork.DEFAULT_SOCKET_IS_BLOCK;
    }
    
    /**  
     * @throws IOException 
     * @constructor：NewServerFactoryBean    
     */ 
    public NewServerFactoryBean(int port) throws IOException {
        //默认为false 非阻塞型
        super((ServerSocketChannel) ServerSocketChannel.open().bind(new InetSocketAddress
                (DefaultNetwork.DEFAULT_NETWORK_LOCAL_PORT)).configureBlocking(DefaultNetwork.DEFAULT_SOCKET_IS_BLOCK),port);
        this.isBlock = DefaultNetwork.DEFAULT_SOCKET_IS_BLOCK;
    }
    
    /**  
     * @throws IOException 
     * @constructor：NewServerFactoryBean    
     */ 
    public NewServerFactoryBean(boolean isblock) throws IOException {
        //默认为false 非阻塞型
        super((ServerSocketChannel) ServerSocketChannel.open().bind(new InetSocketAddress
                (DefaultNetwork.DEFAULT_NETWORK_LOCAL_PORT)).configureBlocking(DefaultNetwork.DEFAULT_SOCKET_IS_BLOCK),DefaultNetwork.DEFAULT_NETWORK_LOCAL_PORT);
        this.isBlock = isblock;
    }

    public T getThreadPool() {
        return threadPool;
    }

    public void setThreadPool(T threadPool) {
        this.threadPool = threadPool;
    }


}
